# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
===========================
@Time : 2022/8/26 0026 22:11
@Author : 测试开发工程师
@File : 12. table_manage_demo.py
@Software: PyCharm
============================
"""
import yaml
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# 实例化 app 对象
from sqlalchemy import *

app = Flask(__name__)

with open("./data.yaml") as f:
    result = yaml.safe_load(f)
    username = result.get('database').get('username')
    password = result.get('database').get('password')
    server = result.get('database').get('server')
    db = result.get('database').get('db')

app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql+pymysql://{username}:{password}@{server}/{db}?charset=utf8'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

# SQLAlchemy 绑定 app
db = SQLAlchemy(app)


# 定义数据库的表，需要继承 db.Model .  db 为 app 启动时的 SQLAlchemy 绑定的实例
# 创建 User 表
class User(db.Model):
    id = Column(Integer, primary_key=True)
    username = Column(String(80))


# 类名，相当于 表名
# 创建 Students 表
class Students(db.Model):
    id = Column(Integer, primary_key=True)
    username = Column(String(80))
    gender = Column(String(80))


# 想要大写的表名  __tablename__
class UserInfo(db.Model):
    # 指定表名 __tablename__ 属性
    __tablename__ = "User"
    id = Column(Integer, primary_key=True)
    username = Column(String(80))


if __name__ == '__main__':
    db.create_all()
    db.drop_all()
